<?php
	session_start();
	$link = mysqli_connect('localhost', 'root', '') or die("Failed to connect to MySQL: " . mysql_error());
	if (mysqli_connect_errno()) {
	        echo 'Failed to connect to MySQL';
	        exit;
	}
	$sql = "USE web_db";
	$query = mysqli_query($link,$sql);


	if($_POST["id"] != ""){
		$id = $_POST["id"];
		$_SESSION['id'] = $id;
	}else{
		$id = $_SESSION['id'];
	}

	if($_POST["vorname"] != ""){
		$vorname = $_POST["vorname"];
		$_SESSION['vorname'] = $vorname;
	}else{
		$vorname = $_SESSION['vorname'];
	}
	
	if($_POST["name"] != ""){
		$name = $_POST["name"];
		$_SESSION['name'] = $name;
	}else{
		$name = $_SESSION['name'];
	}
	
	if($_POST["birth_date"] != ""){
		$b_date = $_POST['birth_date'];
		$_SESSION['b_date'] = $b_date;
	}else{
		$b_date = $_SESSION['b_date'];
	}
    
    if($_POST["location"] != ""){
    	$location = $_POST['location'];
    	$_SESSION['location'] = $location;
    }else{
    	$location = $_SESSION['location'];
    }
	
	if($_POST["pwd"] != ""){
		$pwd = $_POST["pwd"];
		$_SESSION['pwd'] = $pwd;
	}else{
		$pwd = $_SESSION['pwd'];
	}

	echo $id;
	echo "<br>";
	echo $vorname;
	echo "<br>";
	echo $name;
	echo "<br>";

	$old_id = $_SESSION['id'];

	$_SESSION['cambios'] = "si";

	$sql = "UPDATE staff
			SET id = '$id', pwd = '$pwd', vorname = '$vorname', name = '$name', birth_date = '$b_date', location = '$location'
			WHERE id = $old_id";
	$query = mysqli_query($link,$sql);
	header("location:profile.php");
?>